package com.kipling.kio.demo.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.kipling.kio.core.Kio;
import com.kipling.kio.core.intf.Packet;
import com.kipling.kio.demo.common.Command;
import com.kipling.kio.demo.common.ShowcasePacket;
import com.kipling.kio.demo.common.intf.AbsShowcaseBsHandler;
import com.kipling.kio.demo.common.packets.P2PReqBody;
import com.kipling.kio.demo.common.packets.P2PRespBody;
import com.kipling.kio.json.Json;
import com.kipling.kio.server.ServerGroupContext;

/**
 * @author tanyaowu 
 * 2017年3月27日 下午9:51:28
 */
public class P2PReqHandler extends AbsShowcaseBsHandler<P2PReqBody>
{
	private static Logger log = LoggerFactory.getLogger(P2PReqHandler.class);

	/**
	 * 
	 * @author: tanyaowu
	 */
	public P2PReqHandler()
	{
	}

	/**
	 * @param args
	 * @author: tanyaowu
	 */
	public static void main(String[] args)
	{

	}
	/** 
	 * @return
	 * @author: tanyaowu
	 */
	@Override
	public Class<P2PReqBody> bodyClass()
	{
		return P2PReqBody.class;
	}

	

	@Override
	public Object handler(Packet packet, P2PReqBody bsBody,ServerGroupContext<Packet, Object> serverGroupContext) throws Exception {
		log.info("收到点对点请求消息:{}", Json.toJson(bsBody));
		System.out.println("收到点对点请求消息:"+ Json.toJson(bsBody));	
		System.out.println("userid:"+packet.getUserid());
		System.out.println("ToUserid:"+bsBody.getToUserid());
		P2PRespBody p2pRespBody = new P2PRespBody();
		p2pRespBody.setFromUserid(bsBody.getToUserid());		
		p2pRespBody.setText(bsBody.getText());		
		ShowcasePacket respPacket = new ShowcasePacket();
		respPacket.setType(Command.MSG_REQ.getCode());
		respPacket.setContent(p2pRespBody);
		Kio.sendToUser(serverGroupContext, bsBody.getToUserid(), packet);
		return null;
	}
}
